﻿using MediatR;
using SmartCity.ProcessEngine.API.Domain;
using SmartCity.ProcessEngine.API.ErrorCode;
using SmartCity.ProcessEngine.API.Infrastructrue.Extensions;
using SmartCity.ProcessEngine.API.Infrastructrue.Interfaces;
using SmartCity.ProcessEngine.API.Read.Applications.Commands;
using SmartCity.ProcessEngine.API.Read.DTOS;

namespace SmartCity.ProcessEngine.API.Read.Applications.CommandHandlers
{
    /// <summary>
    /// 表单级联命令处理
    /// </summary>
    public class FormCasceQueryCommandHandler : IRequestHandler<FormCasceQueryCommand, APIResult<List<FormDTO>>>
    {
        /// <summary>
        /// 表单仓储
        /// </summary>
        private readonly IBaseRespository<WF_Form> formRep;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="formRep">表单仓储</param>
        public FormCasceQueryCommandHandler(IBaseRespository<WF_Form> formRep)
        {
            this.formRep = formRep;
        }

        /// <summary>
        /// 请求处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public Task<APIResult<List<FormDTO>>> Handle(FormCasceQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<FormDTO>> result=new APIResult<List<FormDTO>>();
            result.Code=APIResultCode.SearchSuccess;
            result.Message=APIResultCode.SearchSuccess.GetDescription();

            var res = from a in formRep.GetAll()
                      where !a.IsDelete && a.TypeNo==request.TypeNo 
                      select new FormDTO
                      {
                          FormNo = a.FormNo,
                          FormName = a.FormName,
                      };
            result.Data=res.ToList();
            return Task.FromResult(result);
        }
    }
}
